package com.jzg.jcptadmin.presenter;

import android.content.Context;

import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.ValuationDetailData;
import com.jzg.jcptadmin.viewinterface.EstimateActivityInterface;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by wangyd on 15/12/22.
 * 及时系统评估逻辑
 */
public class EstimatePresenter extends BasePresenter<EstimateActivityInterface> {
    public static final String TAG = EstimatePresenter.class.getSimpleName();

    private final DataManager mDataManager;
    private Subscription mSubscription;
    private EstimateActivityInterface mView;

    public EstimatePresenter(DataManager dataManager) {
        mDataManager = dataManager;
    }

    @Override
    public void attachView(EstimateActivityInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    public void loadValuationDetail(Context context) {
        checkViewAttached();
        if (!AppContext.isNetWork) return;

        mSubscription = mDataManager.loadValuationDetail(mView.getEstimateParams()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ValuationDetailData>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if(e!=null&&mView!=null){

                    mView.showError(e.toString());
                }
            }

            @Override
            public void onNext(ValuationDetailData datas) {

                mView.toValuationDetailActivity(datas);
            }
        });
    }
}
